-- -----------------------------------------------------------------------------
-- | WonderTek [ 网络无处不在，沟通及时到达 ]
-- -----------------------------------------------------------------------------
-- | Copyright (c) 2012, WonderTek, Inc. All Rights Reserved.
-- -----------------------------------------------------------------------------
-- | Author: xxxx <xxxx@xxxx.com>
-- -----------------------------------------------------------------------------
-- | Desc: 常用功能函数
-- -----------------------------------------------------------------------------

function backBtn(sprite)
    Scene:back()
end

function goHome(sprite)
    Scene:go(Alias.home)
end

function goMail(sprite)
    Scene:setReturn(Scene:getNameByHandle(Sprite:getCurScene()), Alias.mailList)
    Scene:go(Alias.mailList)
end

function goDangZhen(sprite)
    Scene:setReturn(Scene:getNameByHandle(Sprite:getCurScene()), Alias.dangzhengxinxiList)
    Scene:go(Alias.dangzhengxinxiList)
end

function goGongWen(sprite)
    Scene:setReturn(Scene:getNameByHandle(Sprite:getCurScene()), Alias.gongwen)
    Scene:go(Alias.gongwen)
end

function setAllShoworHide(sprite, isShow)
    Sprite:setVisible(sprite, isShow)
    Sprite:setActive(sprite, isShow)
    Sprite:setEnable(sprite, isShow)
end
-- 根据ip地址和端口号组装一个url地址
function getUrl()
    local serverPort = Config:get('serverPort')
    if not serverPort or serverPort == '' then
        Config:set('serverPort', '8090')
        serverPort = '8090'
    end
    serverPort = ":" .. serverPort
    local serverUrl = Config:get('serverUrl')
    if not serverUrl or serverUrl == '' then
        Config:set('serverUrl', '120.209.138.202')
        serverUrl = '120.209.138.202'
    end
    return serverUrl .. serverPort .. '/zyJava/mobile/'
end

-- 获得请求url地址
function getWholeUrl(urlContent, params)
    local url = getUrl()
    local lines = Config:get('lines')
    if not lines and lines ~= '' then
        lines = '10'
    end
    if isPost then
        return url .. urlContent
    end
    if params and params ~= '' then
        return url .. urlContent .. '?' .. 'usercode=' .. Config:get('username') .. '&pagesize=' .. lines.. '&' .. params
    else
        return url .. urlContent .. '?' .. 'usercode=' .. Config:get('username') .. '&pagesize=' .. lines
    end
end

function getJsonArrayCount(data)
    local count = 0
    if data then
        if  #data == 0 and type(data[0]) == 'table' then
            count = 1
        else
            count = #data + 1
        end
    else
    return 0
    end
    return count
end
function getMailUrl(oper,pagesize,curpage)
    local mailreceServer = Config:get('mailreceServer')
    local mailsendServer = Config:get('mailsendServer')
    local mailUser = Config:get('mailuser')
    local mailpwd = Config:get('mailpwd')
    local ip = 'http://120.209.131.146/webcloud/'
    local param = '&mailUser=' .. mailUser .. '&mailPassword=' .. mailpwd
    if oper == 'detail' then
        return ip .. 'client/mail/getMailDetail.html?'..'mailServer=' .. mailreceServer .. param
    elseif oper == 'send' then
        return ip .. 'client/mail/sendMail.html?' ..'mailServer=' .. mailsendServer .. param
    elseif oper == 'upload' then
        return ip .. 'upload/UGC_GetUploadUrl.html?'
    else
        return ip .. 'client/mail/getMailList.html?' ..'mailServer=' .. mailreceServer.. param..'&pageSize='..(pagesize or '')..'&currentPage='..(curpage or '')
    end
end

function getFileNameAndExt(filePath)
    local reversStr = string.reverse(filePath)
    local line = string.find(reversStr, '%/')
    local point = string.find(reversStr, '%.')
    if point and point ~= 1 then
        local fileName
        if line then
            fileName = string.sub(filePath, -line+1, -point-1)
        else
            fileName = string.sub(filePath, 1, -point-1)
        end
        local fileExt = string.sub(filePath, -point+1)
        return fileName, fileExt
    end
    return nil
end

function handleF2Key(sprite)
    if Loading:isShow() then
        Loading:close()
        return
    elseif Dialog:isShow() then
        Dialog:close()
        return
    end
    backBtn()
end

function setSpriteVisible(sprite, visible)
    Sprite:setVisible(sprite, visible)
    Sprite:setEnable(sprite, visible)
end

function doExit()
    IO:dirRemove('WONDER:\\temp', 1)
    IO:dirRemove('CACHE:\\com_wondertek_hsoa', 1)
    Scene:exit()
end

function getCurDateAndTime()
    local year = os.date("*t")["year"]
    local month = os.date("*t")["month"]
    local day = os.date("*t")["day"]
    local hour = os.date("*t")["hour"]
    local minute = os.date("*t")["min"]
    local sec = os.date("*t")["sec"]
    return string.format('%04s-%02s-%02s %02s:%02s:%02s', year, month, day, hour, minute, sec)
end
function Util:stringToTable(s)
    local returnTable = loadstring("return "..s)()
    if type(returnTable) == 'table' then
        return returnTable
    else
        return {}
    end
end
function Util:tableToString(t)
    if type(t) == 'table' then
        return Util:tostring(t, '', '')
    else
        return nil
    end
end
function deleApeopleFromTable(table)
    local tableLenth = #table
    for i=tableLenth,1, -1 do
        local timeLine = Util:getTimeRegionPosition(remindsTable[i].startTime,remindsTable[i].endTime)
        if timeLine == 'current' or timeLine == 'before' then
            table.remove(remindsTable,i)
        end
    end
end    

function utfstrlen(str)
    local len = #str;
    local left = len;
    local cnt = 0;
    local arr={0,0xc0,0xe0,0xf0,0xf8,0xfc};
    while left ~= 0 do
    local tmp=string.byte(str,-left);
    local i=#arr;
    while arr[i] do
    if tmp>=arr[i] then left=left-i;break;end
    i=i-1;
    end
    cnt=cnt+1;
    end
    return cnt;
end

--判断是否为空
function isEmpty(str)
    if str == nil or str == '' then
        return true
    else
        return false
    end
end

function Split(szFullString, szSeparator)
    local nFindStartIndex = 1
    local nSplitIndex = 1
    local nSplitArray = {}
    while true do
        local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
        if not nFindLastIndex then
            local stringData = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
            
            if string.len(stringData) > 0 then
                nSplitArray[nSplitIndex] = stringData
            end
            break
        end
        local stringData = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
        if string.len(stringData) > 0 then
            nSplitArray[nSplitIndex] = stringData
        end
        nFindStartIndex = nFindLastIndex + string.len(szSeparator)
        nSplitIndex = nSplitIndex + 1
    end
    return nSplitArray
end

-- 检查邮箱的合法性
function isRightEmail(str)
     if string.len(str or "") < 6 then return false end
     local b,e = string.find(str or "", '@')
     local bstr = ""
     local estr = ""
     if b then
         bstr = string.sub(str, 1, b-1)
         estr = string.sub(str, e+1, -1)
         Log:write("@jldu bstr:"..bstr)
         Log:write("@jldu estr:"..estr)
     else
         return false
     end
 
     -- check the string before '@'
     -- 首字母为.则返回false
     if string.find(bstr, "^[%.]+") then return false end
     local p1,p2 = string.find(bstr, "[%w_.]+")
     Log:write("@jldu p1:"..p1)
     Log:write("@jldu p2:"..p2)
     if (p1 ~= 1) or (p2 ~= string.len(bstr)) then return false end
     
     -- check the string after '@'
     if string.find(estr, "^[%.]+") then return false end
     if string.find(estr, "%.[%.]+") then return false end
     if string.find(estr, "@") then return false end
     if string.find(estr, "[%.]+$") then return false end
 
     _string,count = string.gsub(estr, "%.", "")
     Log:write("@jldu _string:".._string)
     Log:write("@jldu count:"..count)
     if (count < 1 ) or (count > 3) then
         return false
     end
 
     return true
 end

 -- @ wyw 检查参数的合法性
 function checkParam(paramTable)
     Log:write('进入参数检验',paramTable)
     if paramTable == nil or paramTable == '' then return false end

     if paramTable.usercode == nil or paramTable.usercode == '' then 
        Dialog:show('重要提醒','数据不全','ok')
        return false 
     end
     if paramTable.cmd == nil or paramTable.cmd == '' then 
        Dialog:show('重要提醒','数据不全','ok')
        return false 
     end
     if paramTable.userName == nil or paramTable.userName == '' then 
        Dialog:show('重要提醒','未填写姓名','ok')
        return false 
     end
     if not paramTable.sex == nil or paramTable.sex == '' then
        Dialog:show('重要提醒','未选择性别','ok')
        return false
     end
     if paramTable.cardid == nil or string.len (paramTable.cardid) ~= 18 then
        Log:write('哈哈哈哈哈',string.len (paramTable.cardid),cardid)---some problems
        Dialog:show('重要提醒','身份证号不正确','ok')
        return false
     end
     if paramTable.birth == nil or paramTable.birth == '' then
        Dialog:show('重要提醒','出生日期未填写','ok')
        return false
     end
     
     return true
 end
